/**
 * 标签管理API
 */
import request from '@/api/request'

/**
 * 标签接口
 */
export interface Tag {
  id: number
  name: string
  category: 'facility' | 'environment' | 'special' | 'custom'
  level: 1 | 2 | 3  // 星级优先级
  keywords: string
  description: string
  is_active: boolean
  usage_count: number
  created_at: string
  updated_at: string
}

/**
 * 标签创建数据
 */
export interface TagCreate {
  name: string
  category?: string
  level?: number
  keywords?: string
  description?: string
  is_active?: boolean
}

/**
 * 标签更新数据
 */
export interface TagUpdate {
  name?: string
  category?: string
  level?: number
  keywords?: string
  description?: string
  is_active?: boolean
}

/**
 * 标签统计
 */
export interface TagStatistics {
  total: number
  active: number
  by_category: Record<string, number>
  by_level: Record<string, number>
}

/**
 * 标签匹配结果
 */
export interface TagMatchResult {
  tag_id: number
  tag_name: string
  confidence: number
  level: number
  source: 'ai' | 'rule' | 'manual'
}

/**
 * 获取标签列表
 */
export const getTags = (params?: {
  category?: string
  level?: number
  skip?: number
  limit?: number
}) => {
  return request.get<Tag[]>('v1/tags', { params })
}

/**
 * 获取热门标签
 */
export const getPopularTags = (limit = 20) => {
  return request.get<Tag[]>('v1/tags/popular', {
    params: { limit }
  })
}

/**
 * 获取标签统计
 */
export const getTagStatistics = () => {
  return request.get<TagStatistics>('v1/tags/statistics')
}

/**
 * 获取单个标签详情
 */
export const getTag = (id: number) => {
  return request.get<Tag>(`v1/tags/${id}`)
}

/**
 * 创建标签
 */
export const createTag = (data: TagCreate) => {
  return request.post<Tag>('v1/tags', data)
}

/**
 * 更新标签
 */
export const updateTag = (id: number, data: TagUpdate) => {
  return request.patch<Tag>(`v1/tags/${id}`, data)
}

/**
 * 删除标签（软删除）
 */
export const deleteTag = (id: number) => {
  return request.delete(`v1/tags/${id}`)
}

/**
 * 智能匹配标签
 */
export const matchTags = (text: string, maxTags = 4) => {
  return request.post<TagMatchResult[]>('v1/tags/match', null, {
    params: { text, max_tags: maxTags }
  })
}

/**
 * 标签分类选项
 */
export const TAG_CATEGORIES = [
  { label: '配套设施', value: 'facility' },
  { label: '环境要求', value: 'environment' },
  { label: '特殊需求', value: 'special' },
  { label: '自定义', value: 'custom' }
]

/**
 * 标签优先级选项
 */
export const TAG_LEVELS = [
  { label: '⭐⭐⭐ 高优先级', value: 3 },
  { label: '⭐⭐ 中优先级', value: 2 },
  { label: '⭐ 低优先级', value: 1 }
]

/**
 * 根据分类获取标签颜色
 */
export const getTagColorByCategory = (category: string): 'primary' | 'success' | 'warning' | 'info' | 'danger' => {
  const colorMap: Record<string, 'primary' | 'success' | 'warning' | 'info' | 'danger'> = {
    facility: 'success',
    environment: 'primary',
    special: 'warning',
    custom: 'info'
  }
  return colorMap[category] || 'info'
}

/**
 * 根据标签名称获取类型（用于显示不同颜色）
 */
export const getTagType = (tagName: string): 'primary' | 'success' | 'warning' | 'info' | 'danger' => {
  // 高优先级标签（3星）
  const highPriority = ['近地铁', '学区房', '商圈', '安静', '拎包入住']
  if (highPriority.includes(tagName)) return 'danger'

  // 中优先级标签（2星）
  const mediumPriority = [
    '需要车位', '医院', '公园', '采光好', '朝向南',
    '高楼层', '低楼层', '景观好', '可养宠物', '短租',
    '可注册公司', '押一付一', '独立卫浴', '电梯房'
  ]
  if (mediumPriority.includes(tagName)) return 'warning'

  // 低优先级标签（1星）
  return 'info'
}
